package com.botann.projects.controller;

import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.constants.Constant;
import com.botann.projects.base.BaseController;
import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.constants.Constant;
import com.botann.projects.model.Menu;
import com.botann.projects.service.MenuServiceIntf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Charging
 * MenuController
 * Created by 刘山国 on 2017/11/23.
 * Copyright (c) 2017 刘山国. All rights reserved
 */
@RestController
public class MenuController extends BaseController {

    @Autowired
    private MenuServiceIntf service;

    @Override
    public Class loggerClass() {
        return MenuController.class;
    }

    /**
     * 检测菜单名称重复
     * @return Model
     */
    @RequestMapping(value = API.URL_HAS_SAME_MENU,method = RequestMethod.POST)
    public Model hasSameNameMenu(@RequestParam String json, @RequestParam String filed) {
        Menu menu = jsonToModel(json,Menu.class);
        if (menu.getMenuName() == null || menu.getMenuName().isEmpty()) return successModel("菜单名称不可空!",false);
        try {
            Menu oldMenu = service.loadMenuWithName(menu.getMenuName());
            if (oldMenu != null) {
                if (oldMenu.getId() == menu.getId()) {
                    return successModel(true);
                } else {
                    return successModel("菜单名称不可重复!",false);
                }
            } else {
                return successModel(true);
            }
        } catch (Exception e){
            return failureModel(e);
        }
//    public Model hasSameNameMenu(@RequestParam String menuName) {
//        if (menuName.isEmpty()) return successModel("菜单名称不可空!",true);
//        try {
//            Menu menu = service.loadMenuWithName(menuName);
//            if (menu != null) {
//                return successModel("菜单名称不可重复!",true);
//            } else {
//                return successModel(false);
//            }
//        } catch (Exception e){
//            return failureModel(e);
//        }
    }

    /**
     * 新增或者更新菜单
     * @param menusJson String
     * @param deleteIdsJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_SAVE_UPDATE_MENUS, method = RequestMethod.POST)
    public Model updateMenus(@RequestParam String menusJson, @RequestParam String deleteIdsJson) {
        try {
            service.updateMenus(menusJson,deleteIdsJson);
            return successModel(Constant.SAVE_SUCCESS,Constant.EMPTY);
        } catch (Exception e) {
            return failureModel(e);
        }
    }


    /**
     * 获取所有的菜单
     * @return Model
     */
    @RequestMapping(value = API.URL_ALL_MENUS, method = RequestMethod.POST)
    public Model loadMenus(){
        try {
            return successModel(service.loadMenus());
        } catch (Exception e) {
            return failureModel(e);
        }
    }


    /**
     * 获取某个角色的菜单
     * @param roleId  Integer if null use now user's companyType
     * @return  Model
     */
    @RequestMapping(value = API.URL_ROLE_MENUS, method = RequestMethod.POST)
    public Model loadRoleMenus(Integer roleId) {
        try {
            return successModel(service.loadMenusOfRole(roleId));
        } catch (Exception e) {
            return failureModel(e);
        }
    }

    /**
     * 获取某角色选择的菜单ids
     * @param roleId 角色id
     * @return Model
     */
    @RequestMapping(value = API.URL_ROLE_MENU_IDS, method = RequestMethod.POST)
    public Model loadRoleIds(@RequestParam Integer roleId) {
        try {
            return successModel(service.loadMenuIdsOfRole(roleId));
        } catch (Exception e) {
            return failureModel(e);
        }
    }

    /**
     * 根据角色id更新选择的菜单ids
     * @param menuIdsJson   菜单ids
     * @param roleId  角色id
     * @return Model
     */
    @RequestMapping(value = API.URL_UPDATE_ROLE_MENU_IDS, method = RequestMethod.POST)
    public Model updateRoleMenuWithIds(@RequestParam String menuIdsJson, @RequestParam Integer roleId) {
        try {
            service.updateRoleMenuIds(menuIdsJson,roleId);
            return successModel(Constant.UPDATE_SUCCESS, Constant.EMPTY);
        } catch (Exception e) {
            return failureModel(e);
        }
    }


}
